package cn.iocoder.boot.springai001.controller;

import cn.iocoder.boot.springai001.config.SessionStorageProperties;
import cn.iocoder.boot.springai001.config.SessionStorageValidator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置信息控制器
 * <p>
 * 展示如何在 Controller 中使用 SessionStorageProperties
 */
@CrossOrigin
@Tag(name = "配置信息", description = "查看系统配置信息")
@RestController
@RequestMapping("/api/config")
@RequiredArgsConstructor
public class ConfigInfoController {

    private final SessionStorageProperties storageProperties;
    private final SessionStorageValidator storageValidator;

    /**
     * 获取 Session 存储配置信息
     */
    @Operation(summary = "获取Session存储配置", description = "查看当前使用的Session存储方式")
    @GetMapping("/session-storage")
    public Map<String, Object> getSessionStorageConfig() {
        Map<String, Object> config = new HashMap<>();
        
        // 直接使用 SessionStorageProperties
        config.put("type", storageProperties.getType());
        config.put("isMemoryStorage", storageProperties.isMemoryStorage());
        config.put("isDatabaseStorage", storageProperties.isDatabaseStorage());
        config.put("isValid", storageProperties.isValid());
        
        // 使用验证器获取更多信息
        config.put("description", storageValidator.getStorageTypeDescription());
        config.put("productionReady", storageValidator.isProductionReady());
        
        // 添加建议
        if (storageProperties.isMemoryStorage()) {
            config.put("recommendation", "开发环境配置正确。生产环境建议配置: session.storage.type=database");
        } else {
            config.put("recommendation", "生产环境配置正确");
        }
        
        return config;
    }

    /**
     * 获取所有配置信息
     */
    @Operation(summary = "获取系统配置概览", description = "查看系统的主要配置信息")
    @GetMapping("/overview")
    public Map<String, Object> getConfigOverview() {
        Map<String, Object> overview = new HashMap<>();
        
        // Session 存储配置
        Map<String, String> sessionConfig = new HashMap<>();
        sessionConfig.put("storageType", storageProperties.getType());
        sessionConfig.put("description", storageValidator.getStorageTypeDescription());
        overview.put("sessionStorage", sessionConfig);
        
        // 可以添加其他配置信息
        overview.put("version", "1.0.0");
        overview.put("environment", storageProperties.isMemoryStorage() ? "development" : "production");
        
        return overview;
    }
}

